//
//  NearbyStadiumCell.m
//  GolfFriend
//
//  Created by Vescky on 14-3-2.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "NearbyStadiumCell.h"
#import "CommonUtility.h"
#import "AppsLocationManager.h"
#import "UIImageView+MJWebCache.h"
#import "AppDelegate.h"
#import "AppsEngine.h"
#import "SVProgressHUD.h"
#import "UserSession.h"
#import "ZanDao.h"
#import "StadiumDetailViewController.h"

@interface NearbyStadiumCell () {
    NSMutableDictionary *stadiumInfo;
}

@end

@implementation NearbyStadiumCell

@synthesize isSupported,imgvAvatar;

- (void)setDataInfo:(NSDictionary*)dict {
    stadiumInfo = [[NSMutableDictionary alloc] initWithDictionary:dict];
    labelStadiumName.text = [stadiumInfo objectForKey:@"name"];
    labelPrice.text = [NSString stringWithFormat:@"%@%@",[stadiumInfo objectForKey:@"price"],[stadiumInfo objectForKey:@"unit"]];
    
//    CLLocation *bpLocation = getGfLocation([stadiumInfo objectForKey:@"position"]);
//    int kilometers = [[AppsLocationManager sharedManager] getDistance:bpLocation] / 1000;
//    labelDistance.text = [NSString stringWithFormat:@"%.2fKm",[[stadiumInfo objectForKey:@"distance"] floatValue]];
    labelDistance.text = getFormatedDistance([[stadiumInfo objectForKey:@"distance"] doubleValue] * 1000);
    
    labelComment.text = [stadiumInfo objectForKey:@"comment"];
    labelZan.text = [stadiumInfo objectForKey:@"zan"];
    labelDescription.text = removeHtmlTags([stadiumInfo objectForKey:@"describe"]);
    
    NSArray *imgArr = [[stadiumInfo objectForKey:@"picLink"] objectForKey:@"image"];
    if (imgArr && [imgArr count] > 0) {
        [imgvAvatar setImageURLStr:[imgArr objectAtIndex:0] placeholder:Default_Image_Normal];
    }
    
    switch ([[stadiumInfo objectForKey:@"isSupported"] intValue]) {
        case 0:
            //未赞
            btnZan.selected = NO;
            break;
        case 1:
            //已赞
            btnZan.selected = YES;
            break;
        case 2:
            //自己
            btnZan.enabled = NO;
            break;
        default:
            break;
    }
    
}

- (IBAction)btnAction:(id)sender {
    UIButton *btn = (UIButton*)sender;
    if (btn.tag == 101) {
        //赞
        if (isSupported) {
            [SVProgressHUD showErrorWithStatus:@"您已赞过!"];
            return;
        }
        NSString *stadiumId = [stadiumInfo objectForKey:@"id"];
        NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:stadiumId,@"articleId",@"stadium",@"type", nil];

        [[AppsEngine shareAppsEngine] submitRequest:@"gfPublishZan" param:params onCompletion:^(id jsonResponse) {
            NSLog(@"zan success!");
        } onError:^(NSError *error) {
            
        } method:@"GET"];
        
        //缓存到本地
        NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:[[UserSession shareUserSession] getUserId],@"userId",
                              @"stadium",@"type",stadiumId,@"recordId",nil];
        [[ZanDao shareZanDao] saveZanInfo:dict];
        isSupported = YES;
        labelZan.text = [NSString stringWithFormat:@"%d",[labelZan.text intValue]+1];
        [btnZan setSelected:YES];
        
    }
    else if (btn.tag == 102) {
        //评论
        StadiumDetailViewController *sDetailVc = [[StadiumDetailViewController alloc] init];
        sDetailVc.detailInfo = [[NSMutableDictionary alloc] initWithDictionary:stadiumInfo];
        sDetailVc.webappLink = [NSString stringWithFormat:@"http://mingyuanhui.sinaapp.com/index.php/webview/index/qiuchang?id=%@",[stadiumInfo objectForKey:@"id"]];
        sDetailVc.coverImage = imgvAvatar.image;
        [[APP_DELEGATE navigationController] pushViewController:sDetailVc animated:YES];
    }
}



@end
